package com.zxl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mysql.jdbc.StringUtils;
import com.zxl.entity.User;
import com.zxl.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @packageName com.zxl.controller
 * @ClassName
 * @Author zxl
 * @Description //TODO
 * @date
 **/
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/add")
    public boolean add(@RequestBody User user) {
        user.setCreateTime(new Date());
        return userService.save(user);
    }

    @GetMapping("/list")
    public List<User> getUser() {
        return userService.list();
    }

    @PostMapping("/delete")
    public boolean delete(@RequestParam("ids") Integer[]ids) {
        List<Integer> list = Arrays.asList(ids);
        boolean b = userService.removeByIds(list);
        return b;
    }

    @GetMapping("/getById")
    public User getById(@RequestParam("id") Integer id) {
        return userService.getById(id);
    }

    @PostMapping("/edit")
    public Boolean edit(@RequestBody User user) {

        return userService.updateById(user);
    }

    @PostMapping("/findBySome")
    public List<User> findBySome(@RequestBody User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (!StringUtils.isNullOrEmpty(user.getUsername())) {
            wrapper.like("username", user.getUsername());
        }
        if (!StringUtils.isNullOrEmpty(user.getEmail())) {
            wrapper.eq("email", user.getEmail());
        }
        if (!StringUtils.isNullOrEmpty(user.getPhone())) {
            wrapper.eq("phone",user.getPhone());
        }
        return userService.list(wrapper);
    }

    @RequestMapping("/checkUsername")
    public User checkUsername(@RequestParam("param") String param){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",param);
        return userService.getOne(wrapper);
    }

    @RequestMapping("/findByoauthId")
    public User findByoauthId(@RequestParam("oauthId") String oauthId){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("oauth_id",oauthId);
        return userService.getOne(wrapper);
    }

}
